package a07_二叉树;

/**
 * <p>
 * a23_验证二叉搜索树
 * </p>
 * 验证的东西？
 *  中序遍历肯定是有序的
 * 误区？
 *  中节点要比左边【所有】的节点大，比右边【所有】的节点小
 *
 * @author flyduck
 * @since 2024/12/18
 */
public class a23_验证二叉搜索树复习1 {

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(3,
                new TreeNode(1,new TreeNode(0),new TreeNode(2)),
                new TreeNode(5,new TreeNode(4),new TreeNode(6)));
        a23_验证二叉搜索树复习1 test = new a23_验证二叉搜索树复习1();
        boolean validBST = test.isValidBST(treeNode);
        System.out.println();
    }

    private TreeNode prevNode;

    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }

        boolean leftValidBST = isValidBST(root.left);

        if(prevNode != null && root.val <= prevNode.val){
            return false;
        }

        prevNode = root;

        boolean rightValidBST = isValidBST(root.right);

        return leftValidBST && rightValidBST;
    }
}
